import { login, scanlogin, logout, getInfo } from '@/api/user/user'
import { getModulesTree } from '@/api/index'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    modules: null,
    name: '',
    avatar: '',
    unionid: '',
    unitid: '',
    unitname: '',
    userdepart: {},
    userUnit: []
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_UnionId: (state, unionid) => {
    state.unionid = unionid
  },
  SET_UnitId: (state, id) => {
    state.unitid = id
  },
  SET_UnitName: (state, unitname) => {
    state.unitname = unitname
  },
  SET_MODULES: (state, modules) => {
    state.modules = modules
  },
  SET_USERDEPART: (state, userdepart) => {
    state.userdepart = userdepart
  },
  SET_USERUNIT: (state, userUnit) => {
    state.userUnit = userUnit
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { Account, Pwd } = userInfo
    return new Promise((resolve, reject) => {
      login({ Account: Account.trim(), Pwd: Pwd }).then(response => {
        const { Data, Msg } = response
        commit('SET_TOKEN', Data.Token)
        setToken(Data.Token)
        resolve(Msg)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // // user login
  // wxlogin({ commit }, code) {
  //   return new Promise((resolve, reject) => {
  //     wxlogin({ code: code.trim() }).then(response => {
  //       const { Data } = response
  //       commit('SET_TOKEN', Data.Token)
  //       setToken(Data.Token)
  //       resolve()
  //     }).catch(error => {
  //       reject(error)
  //     })
  //   })
  // },
  scanlogin({ commit }, uid) {
    return new Promise((resolve, reject) => {
      scanlogin({ uid: uid.trim() }).then(response => {
        const { Data, Msg } = response
        commit('SET_TOKEN', Data.Token)
        setToken(Data.Token)
        resolve(Msg)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { Data } = response

        if (!Data) {
          reject('请重新登录')
        }
        var departId = Data.CurrUnitDepartmentUserList && Data.CurrUnitDepartmentUserList.length > 0 ? Data.CurrUnitDepartmentUserList[0].DepartmentId : ''
        var departName = Data.CurrUnitDepartmentUserList && Data.CurrUnitDepartmentUserList.length > 0 ? Data.CurrUnitDepartmentUserList[0].DepartmentName : ''
        var userdepart = { UserId: Data.UserInfo.Id, UserName: Data.UserInfo.UserName, DepartmentId: departId, DepartmentName: departName }
        commit('SET_NAME', Data.UserInfo.UserName || Data.UserInfo.NickName)
        commit('SET_AVATAR', Data.UserInfo.Avatar)
        commit('SET_UnionId', Data.UserInfo.UnionId)
        commit('SET_USERDEPART', userdepart)
        commit('SET_USERUNIT', Data.UnitUsers)
        if (Data.CurrUnit) {
          commit('SET_UnitName', Data.CurrUnit.UnitName)
          commit('SET_UnitId', Data.CurrUnit.UnitId)
        }
        resolve(Data.UserInfo)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 获取用户模块
  GetModulesTree({ commit, state }) {
    return new Promise((resolve, reject) => {
      // if (state.modules != null) {
      //   resolve(state.modules)
      //   return
      // }
      getModulesTree().then(response => {
        // resetRouter()
        commit('SET_MODULES', response.Data)
        resolve(response.Data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  ReloadMenu({ commit, state }) {
    return new Promise((resolve, reject) => {
      // if (state.modules != null) {
      //   resolve(state.modules)
      //   return
      // }
      getModulesTree().then(response => {
        // resetRouter()
        commit('SET_MODULES', response.Data)
        resolve(response.Data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 前端 登出
  FedLogOut({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      removeToken()
      resolve()
    })
  },
  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

